package com.iteaj.iot.client.proxy;

import com.iteaj.iot.client.ClientProperties;
import com.iteaj.iot.client.SocketClient;
import com.iteaj.iot.client.TcpSocketClient;
import com.iteaj.iot.client.component.TcpClientComponent;
import com.iteaj.network.IotProtocolFactory;
import com.iteaj.network.client.IotClient;
import com.iteaj.network.config.ConnectProperties;
import com.iteaj.network.proxy.ProxyClientMessage;
import org.springframework.beans.BeanUtils;

public class ProxyClientComponent extends TcpClientComponent<ProxyClientMessage> {

    public ProxyClientComponent(ClientProperties.ClientProxy config) {
        super(config);
    }

    @Override
    public TcpSocketClient createNewClient(ConnectProperties config) {
        ClientProperties.ClientProxy clientProxy;
        if(config instanceof ClientProperties.ClientProxy) {
            clientProxy = (ClientProperties.ClientProxy) config;
        } else {
            clientProxy = new ClientProperties.ClientProxy();
            BeanUtils.copyProperties(getConfig(), clientProxy);
            clientProxy.setHost(config.getHost());
            clientProxy.setPort(config.getPort());
        }

        return new ProxyClient(this, clientProxy);
    }

    @Override
    protected IotProtocolFactory createProtocolFactory() {
        return new ProxyClientProtocolFactory();
    }

    @Override
    public String getName() {
        return "代理客户端";
    }

    @Override
    public String getDesc() {
        return "用于代理对设备的操作";
    }

}
